﻿package core.drawing
{
    import core.enum.*;
    import flash.display.*;
    import flash.events.*;

    final public class SpongeTool extends ShapeTool
    {
        var source:BitmapData;
        var px:uint;
        var f:Number;
        var tf:Number;
        var s:Number;
        var low:int;
        var high:int;
        var diff:int;
        var intensity:int;
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var alpha:int;
        var a:int;
        var r:int;
        var g:int;
        var b:int;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;

        public function SpongeTool(param1:Tuyitu)
        {
            super(param1, ToolType.Sponge);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                this.Prepare(Action.SpongeTool, layer.activeBitmap);
                if (target != null)
                {
                    this.source = layer.bitmap.bitmapData.clone();
                }
                if (!event.shiftKey)
                {
                    var _loc_2:int = -1;
                    y = -1;
                    x = _loc_2;
                }
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(UpdateReferences:int, UpdateReferences:int) : void
        {
            if (this.source != null)
            {
                this.n = 0;
                this.sy = UpdateReferences - int(shape.height * 0.5);
                this.cy = this.sy + shape.height;
                while (this.sy < this.cy)
                {
                    
                    this.sx = UpdateReferences - int(shape.width * 0.5);
                    this.cx = this.sx + shape.width;
                    while (this.sx < this.cx)
                    {
                        
                        this.alpha = shape.data[this.n];
                        if (this.alpha > 0 && (selection == null || selection.IsSelected(this.sx + offset.x, this.sy + offset.y)))
                        {
                            this.px = this.source.getPixel32(this.sx, this.sy);
                            this.a = this.px >>> 24 & 255;
                            if (this.a > 0)
                            {
                                this.r = this.px >>> 16 & 255;
                                this.g = this.px >>> 8 & 255;
                                this.b = this.px & 255;
                                this.low = Math.min(this.r, this.g, this.b);
                                this.high = Math.max(this.r, this.g, this.b);
                                this.s = (this.high - this.low) / this.high;
                                if (options.strength < 0)
                                {
                                    this.f = (this.s + (options.strength + 100)) * 1024 * 0.01;
                                }
                                else
                                {
                                    this.f = (this.s + (options.strength * 3 + 100)) * 1024 * 0.01;
                                }
                                this.intensity = 7471 * this.b + 38470 * this.g + 19595 * this.r >> 16;
                                this.r = this.intensity * 1024 + (this.r - this.intensity) * this.f >> 10;
                                this.g = this.intensity * 1024 + (this.g - this.intensity) * this.f >> 10;
                                this.b = this.intensity * 1024 + (this.b - this.intensity) * this.f >> 10;
                                if (this.r > 255)
                                {
                                    this.r = 255;
                                }
                                else if (this.r < 0)
                                {
                                    this.r = 0;
                                }
                                if (this.g > 255)
                                {
                                    this.g = 255;
                                }
                                else if (this.g < 0)
                                {
                                    this.g = 0;
                                }
                                if (this.b > 255)
                                {
                                    this.b = 255;
                                }
                                else if (this.b < 0)
                                {
                                    this.b = 0;
                                }
                                if (this.alpha < 255)
                                {
                                    this.px = target.getPixel32(this.sx, this.sy);
                                    this.ta = this.px >>> 24 & 255;
                                    this.tr = this.px >>> 16 & 255;
                                    this.tg = this.px >>> 8 & 255;
                                    this.tb = this.px & 255;
                                    this.f = this.alpha / 255;
                                    this.tf = 1 - this.f;
                                    this.tr = this.r * this.f + this.tr * this.tf;
                                    this.tg = this.g * this.f + this.tg * this.tf;
                                    this.tb = this.b * this.f + this.tb * this.tf;
                                    target.setPixel32(this.sx, this.sy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                                }
                                else
                                {
                                    target.setPixel(this.sx, this.sy, this.a << 24 | this.r << 16 | this.g << 8 | this.b);
                                }
                            }
                        }
                        var _loc_3:String = this;
                        var _loc_4:* = this.n + 1;
                        _loc_3.n = _loc_4;
                        var _loc_3:String = this;
                        var _loc_4:* = this.sx + 1;
                        _loc_3.sx = _loc_4;
                    }
                    var _loc_3:String = this;
                    var _loc_4:* = this.sy + 1;
                    _loc_3.sy = _loc_4;
                }
            }
            return;
        }// end function

    }
}
